package com.hmall.search.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.common.feign.ItemClient;
import com.hmall.common.feign.UserClient;
import com.hmall.search.mapper.OrderMapper;
import com.hmall.search.pojo.Order;
import com.hmall.search.pojo.OrderDetail;
import com.hmall.search.pojo.OrderLogistics;
import com.hmall.search.pojo.OrderPayload;
import com.hmall.search.service.IOrderService;
import com.hmall.search.service.OrderDetailService;
import com.hmall.search.service.OrderLogisticsService;
import com.hmall.search.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.ArrayList;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderLogisticsService orderLogisticsService;

    @Override
    public Order submit(@RequestBody OrderPayload orderPayload) {
        //获得下单商品ID
        Long itemId = orderPayload.getItemId();
        //下单商品数量
        Integer num = orderPayload.getNum();
        //商品下单状态
        Integer paymentType = orderPayload.getPaymentType();
        Long addressId = orderPayload.getAddressId();//获得下单商品信息
        Item item = itemClient.getById(itemId);
        Long price = item.getPrice();
        //计算下单商品总价格
        Long totalFee;
        totalFee = (price * num);

        //封装Order
        Order order = new Order();

        long id = IdWorker.getId();
        order.setId(id);

        String useId = ThreadLocalUtils.getUseId();
        order.setTotalFee(totalFee);
        order.setPaymentType(paymentType);
        order.setUserId(Long.valueOf(useId));
        order.setStatus(1);
        order.setCreateTime(LocalDateTime.now());
        //保存到数据库
        this.save(order);


        ArrayList<OrderDetail> list = new ArrayList<>();

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setItemId(itemId);
        orderDetail.setNum(num);
        orderDetail.setTitle(item.getName());
        orderDetail.setPrice(item.getPrice());
        //商品规格数据

        orderDetail.setImage(item.getImage());
        list.add(orderDetail);

        orderDetailService.saveBatch(list);

        Address address = userClient.findAddressById(addressId);
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(id);
        //   orderLogistics.setLogisticsNumber(UUID.randomUUID().toString());
        orderLogistics.setContact(address.getContact());
        orderLogistics.setPhone(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());


        orderLogisticsService.save(orderLogistics);

        return order;

    }


}
